﻿using System;
using G.Model;
using System.Collections.Generic;

namespace G.Runtime
{

    public class IntGameValue : GameValue
    {
        private int _Value;

        internal IntGameValue(GameSpace game)
            : base(new GameValueDef("undef", TypeCode.Int32))
        { }

        public IntGameValue(GameSpace game, int x) : this(game)
        {
            this._Value = x;
        }

        internal IntGameValue(GameValueDef def)
            : base(def) 
        { }

        public virtual int Value
        {
            get { return _Value; }
            set 
            {
                if (_Value != value) 
                {
                    _Value = value;
                    FireChangedEvent();
                }
            }
        }

        protected override string FormatDisplayableValue()
        {
            return _Value.ToString();
        }

//        public static implicit operator IntGameValue(int x)
//        {
//            return new IntGameValue(x);
//        }
        
        public static IntGameValue operator +(IntGameValue c1, IntGameValue c2)
        {
            c1.Value += c2.Value;
            return c1;
        }

        public override string ToString()
        {
            return "" + Def.Name + "=" + Value;
        }
        
    }

    public class StringGameValue : GameValue
    {
        private string _Value = "";

        internal StringGameValue(GameSpace game)
            : base(new GameValueDef("null", TypeCode.Int32))
        { }

        public StringGameValue(GameSpace game, string x) : this(game)
        {
            this._Value = x;
        }

        public StringGameValue(GameValueDef def)
            : base(def) 
        { }

        public string Value
        {
            get { return _Value; }
            set 
            {
                _Value = value;
                FireChangedEvent();
            }
        }

        protected override string FormatDisplayableValue()
        {
            return "" + _Value;
        }

        public static StringGameValue operator +(StringGameValue c1, StringGameValue c2)
        {
            c1.Value += c2.Value;
            return c1;
        }

    }
    
    public class BooleanGameValue : GameValue
    {
        private bool _Value;

        internal BooleanGameValue(GameSpace game)
            : base(new GameValueDef("undef", TypeCode.Int32))
        { }

        public BooleanGameValue(GameSpace game, bool x) : this(game)
        {
            this._Value = x;
        }

        internal BooleanGameValue(GameValueDef def)
            : base(def) 
        {
            bool v;
            Boolean.TryParse(def.Value, out v);
            Value = v;
        }

        public bool Value
        {
            get { return _Value; }
            set 
            {
                if (_Value != value)
                {
                    _Value = value;
                    FireChangedEvent();
                }
            }
        }

        protected override string FormatDisplayableValue()
        {
            return _Value.ToString();
        }

    }
    
}
